namespace Microsoft.Robotics.Hardware.Visualization
{
    using System.Runtime.Serialization;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using Microsoft.Robotics.Hardware.Runtime.Sensors;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Visualization;

    /// <summary>
    /// A model for visualization of onboard sensors (i.e. gyro, accelerometer, etc).
    /// </summary>
    [DataContract]
    public sealed class SensorModelAgent : ModelAgent<AttitudeSensorMessage, SensorModel>
    {
        /// <summary>
        /// Initializes a new instance of the SensorModelAgent class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">The name of the agent we subscribe to.</param>
        /// <param name="maxUpdateIntervalMilliseconds">The interval on which to issue data binding updates to the UI</param>        
        public SensorModelAgent(
            string name,
            IProducer<AttitudeSensorMessage> producer, 
            int maxUpdateIntervalMilliseconds)
            : base(name, producer.Name, maxUpdateIntervalMilliseconds)
        {
        }

        /// <summary>
        /// Initialize the agent. 
        /// </summary>
        /// <param name="locator">The locator to use.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);            
        }

        /// <summary>
        /// Update the model when a new local map is received
        /// </summary>
        /// <param name="msg">State containing a local map</param>
        /// <returns>True if the model was updated</returns>
        protected override bool UpdateModel(AttitudeSensorMessage msg)
        {
            bool updated = false;

            if (msg != null)
            {
                this.Model.AttitudeText = string.Format("Attitude: Pith:{0}, Yaw:{1}, Tilt:{2}", msg.State.PitchInRads, msg.State.YawInRads, msg.State.RollInRads);

                updated = true;
            }

            return updated;
        }       
    }
}
